﻿Public Class ImgControl

    Private Rezise_fin As Boolean = False '用户改变图元大小的操作是否完成

    Public Sub New()
        InitializeComponent()

        Init()
    End Sub

    Public Event DeleteEvent As EventHandler

    '用户删除图元
    Private Sub ImgControl_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.Delete Then
            RaiseEvent DeleteEvent(Me, New EventArgs)
        End If
    End Sub


    Private Sub ImgControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


        AddHandler Me.DoubleClick, AddressOf AllControl_DoubleClick
        For Each c As Control In Me.Controls
            AddHandler c.DoubleClick, AddressOf AllControl_DoubleClick
        Next

        'If _ici IsNot Nothing Then
        '    SetMsg()
        'End If

    End Sub

    Public Event ShowDetailsEvent As EventHandler

    Private Sub AllControl_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
        RaiseEvent ShowDetailsEvent(Me, New EventArgs)
    End Sub

    '包括的imgControlItem对象(与数据库进行映射)
    Private _ici As ImgControlItem
    Public Property ici() As ImgControlItem
        Get
            Return _ici
        End Get
        Set(ByVal value As ImgControlItem)
            _ici = value
            SetMsg()
        End Set
    End Property

    '读取或设置当前的温湿度值
    Private _value As String
    Public Property value() As String
        Get
            Return Me.lblvalue.Text
        End Get
        Set(ByVal value As String)
            'If value = "正常" OrElse value = "报警" Then
            If IsNumeric(value) = False Then '是否为数字
                Me.lblvalue.ForeColor = IIf(value = "正常", Color.Black, Color.Red)
                Me.lblvalue.Text = value
            Else
                Me.lblvalue.Text = String.Format(CInt(value * 100) / 100.0, "0.00")
            End If

            RefreshControlSize()
        End Set
    End Property

    '当前是否出于设计状态 
    Private _IsDesign As Boolean
    Public Property IsDesign() As Boolean
        Get
            Return _IsDesign
        End Get
        Set(ByVal value As Boolean)
            _IsDesign = value
        End Set
    End Property


    Private Sub SetMsg()

        Me.TitleMsg.Text = _ici.TitleMsg
        Me.UnitMsg.Text = _ici.UnitMsg
        Me.ToolTip1.SetToolTip(Me, _ici.TipMsg)
        If _ici.DataType = 1 OrElse _ici.DataType = 2 Then '温湿度类型
            Me.lblvalue.Text = "##.##"
        ElseIf _ici.DataType = 3 Then '报警类型
            Me.lblvalue.Text = "无数据"
        End If

        For Each ctl As Control In Me.Controls
            Me.ToolTip1.SetToolTip(ctl, _ici.TipMsg)
        Next

        RefreshControlSize()
    End Sub

    Private Sub RefreshControlSize()
        Dim m_width, m_height As Integer
        m_width = TitleMsg.Location.X + TitleMsg.Width + lblvalue.Width + UnitMsg.Width
        m_height = Math.Max(Math.Max(TitleMsg.Height, lblvalue.Height), UnitMsg.Height)
        Me.Size = New Size(m_width, m_height)
        TitleMsg.Location = New Point(0, (Me.Size.Height - TitleMsg.Size.Height) * 0.5)
        lblvalue.Location = New Point(TitleMsg.Location.X + TitleMsg.Width, (Me.Size.Height - lblvalue.Size.Height) * 0.5 + 2)
        UnitMsg.Location = New Point(lblvalue.Location.X + lblvalue.Size.Width, (Me.Size.Height - UnitMsg.Size.Height) * 0.5 + 2)

    End Sub

    Private Sub Init()

        Me.TitleMsg.Text = ""
        Me.UnitMsg.Text = ""
        Me.ToolTip1.SetToolTip(Me, "")
        For Each ctl As Control In Me.Controls
            Me.ToolTip1.SetToolTip(ctl, "")
        Next

        _IsDesign = True

    End Sub

    Private Sub ImgControl_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
        Rezise_fin = True
    End Sub

    Private Sub ImgControl_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
        If _IsDesign = False Then '主工程图显示中不进行刷新
            Exit Sub
        End If

        'If Rezise_fin = False Then
        '    Exit Sub
        'End If

        'RefreshControlSize()

        Rezise_fin = True
    End Sub

End Class

